Sampling methods
Grid sampling
Every possible combination, hyperparameter must be discrete
from azure.ai.ml.sweep import Choice
command_job_for_sweep = command_job(
batch_size=Choice(values=[16, 32, 64]),
learning_rate=Choice(values=[0.01, 0.1, 1.0]),
)
sweep_job = command_job_for_sweep.sweep(
sampling_algorithm = "grid",
...
)
Random sampling
A mix of discrete and continuous values as shown in the following code example:
from azure.ai.ml.sweep import Normal, Uniform
command_job_for_sweep = command_job(
batch_size=Choice(values=[16, 32, 64]),
learning_rate=Normal(mu=10, sigma=3),
)
sweep_job = command_job_for_sweep.sweep(
sampling_algorithm = "random",
...
)
Sobol
Random sampling but reproduceable with a Seed:
from azure.ai.ml.sweep import RandomSamplingAlgorithm
sweep_job = command_job_for_sweep.sweep(
sampling_algorithm = RandomSamplingAlgorithm(seed=123, rule="sobol"),
...
)
Bayesian sampling
Select combination based on previous select for improved perfomance
from azure.ai.ml.sweep import Uniform, Choice
command_job_for_sweep = job(
batch_size=Choice(values=[16, 32, 64]),
learning_rate=Uniform(min_value=0.05, max_value=0.1),
)
sweep_job = command_job_for_sweep.sweep(
sampling_algorithm = "bayesian",
...
)
Early Termination Policy
Bandit policy
Abandons a trial if the target performance metric underperforms the best trial so far by a specified margin.
from azure.ai.ml.sweep import BanditPolicy
sweep_job.early_termination = BanditPolicy(
slack_amount = 0.2,
delay_evaluation = 5,
evaluation_interval = 1
)
Median stopping policy
Abandons trials where the target performance metric is worse than the median of the running averages for all trials.
from azure.ai.ml.sweep import MedianStoppingPolicy
sweep_job.early_termination = MedianStoppingPolicy(
delay_evaluation = 5,
evaluation_interval = 1
)
Truncation selection policy
A truncation selection policy cancels the lowest performing X% of trials at each evaluation interval based on the truncation_percentage value you specify for X.
from azure.ai.ml.sweep import TruncationSelectionPolicy
sweep_job.early_termination = TruncationSelectionPolicy(
evaluation_interval=1,
truncation_percentage=20,
delay_evaluation=4
)
Sweep Job
The following example script trains a logistic regression model using a --regularization
argument to set the regularization rate hyperparameter, and logs the accuracy metric with the name Accuracy
:
import argparse
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import mlflow
# get regularization hyperparameter
parser = argparse.ArgumentParser()
parser.add_argument('--regularization', type=float, dest='reg_rate', default=0.01)
args = parser.parse_args()
reg = args.reg_rate
# load the training dataset
data = pd.read_csv("data.csv")
# separate features and labels, and split for training/validatiom
X = data[['feature1','feature2','feature3','feature4']].values
y = data['label'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
# train a logistic regression model with the reg hyperparameter
model = LogisticRegression(C=1/reg, solver="liblinear").fit(X_train, y_train)
# calculate and log accuracy
y_hat = model.predict(X_test)
acc = np.average(y_hat == y_test)
mlflow.log_metric("Accuracy", acc)
Configure and run a sweep job
To prepare the sweep job, you must first create a base command job that specifies which script to run and defines the parameters used by the script:
from azure.ai.ml import command
# configure command job as base
job = command(
code="./src",
command="python train.py --regularization ${{inputs.reg_rate}}",
inputs={
"reg_rate": 0.01,
},
environment="AzureML-sklearn-0.24-ubuntu18.04-py37-cpu@latest",
compute="aml-cluster",
)
You can then override your input parameters with your search space:
from azure.ai.ml.sweep import Choice
command_job_for_sweep = job(
reg_rate=Choice(values=[0.01, 0.1, 1]),
)
from azure.ai.ml import MLClient
# apply the sweep parameter to obtain the sweep_job
sweep_job = command_job_for_sweep.sweep(
compute="aml-cluster",
sampling_algorithm="grid",
primary_metric="Accuracy",
goal="Maximize",
)
# set the name of the sweep job experiment
sweep_job.experiment_name="sweep-example"
# define the limits for this sweep
sweep_job.set_limits(max_total_trials=4, max_concurrent_trials=2, timeout=7200)
# submit the sweep
returned_sweep_job = ml_client.create_or_update(sweep_job)